import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2025-03-05
 * Time: 22:57
 */
public class Main {
    public boolean isSan(int a,int b, int c){
        if(a+b>c){
            return true;
        }else{
            return false;
        }
    }
    //自己的
    public int triangleNumber(int[] nums) {
        Arrays.sort(nums);
        int count = 0;
        for(int i = nums.length-1;i >= 2;i--){
            int right = i-1;
            int left = 0;

            while(left < right){
                if(isSan(nums[left],nums[right],nums[i])){
                    count += (right-left);
                    right--;
                }else{
                    left++;
                }
            }
        }
        return count;
    }

    //老师的
    public int triangleNumber1(int[] nums){
        //1.优化，排序
        Arrays.sort(nums);

        //2.利用双指针解决问题
        int ret = 0,n = nums.length;
        for (int i = n - 1; i >= 2; i--) {
            //利用双指针快速统计出符合要求的三元组的个数
            int left = 0,right = i - 1;
            while (left < right){
                if (nums[left] + nums[right] > nums[i]){
                    ret += right - left;
                    right--;
                }else {
                    left++;
                }
            }
        }
        return ret;
    }
}
